package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;

import controllerInterfaces.IControllerBenchmarkPanel;

/**
 * View to establish the parameters of the execution of the tests and then display them using a table.
 * 
 * @author Andy Cullache
 */
public class DialogSetupRunTest extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JFormattedTextField nUser;
	private IControllerBenchmarkPanel myController;


	/**
	 * Principal constructor
	 * 
	 * @param controller      panel controller benchmark.
	 */
	public DialogSetupRunTest(IControllerBenchmarkPanel controller) {
		this.myController = controller;
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("Setup Run Test");
		setResizable(false);
		setBounds(100, 100, 397, 205);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel lblNewLabel = new JLabel("Number User for Test         :");
		
		JLabel lblNumber = new JLabel("Percentage Split (dataset)  :");
		
		nUser = new JFormattedTextField();
		nUser.setValue(new Integer(10));
		nUser.setColumns(3);
		
		JLabel lbltrainningSet = new JLabel("% (training set)");
		
		final JSpinner spinner = new JSpinner();
		spinner.setModel(new SpinnerNumberModel(50, 1, 99, 1));
		
		JLabel lblMaxRecommendedItems = new JLabel("Max. Recommended items :");
		
		final JFormattedTextField maxItems = new JFormattedTextField();
		maxItems.setText("100");
		maxItems.setColumns(3);
		
		JLabel lblNewLabel_1 = new JLabel("Recommended items increased :");
		
		final JFormattedTextField incrementRecommendation = new JFormattedTextField();
		incrementRecommendation.setText("10");
		incrementRecommendation.setColumns(3);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_1)
						.addComponent(lblMaxRecommendedItems)
						.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
						.addComponent(lblNumber, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
						.addComponent(incrementRecommendation)
						.addComponent(maxItems)
						.addComponent(nUser)
						.addComponent(spinner))
					.addPreferredGap(ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
					.addComponent(lbltrainningSet, GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(nUser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lbltrainningSet, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
						.addComponent(spinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNumber))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMaxRecommendedItems)
						.addComponent(maxItems, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(11)
							.addComponent(lblNewLabel_1))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(incrementRecommendation, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		JButton okButton;
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						myController.runTestList(Integer.parseInt(nUser.getText()), Integer.parseInt(""+spinner.getValue()),
								Integer.parseInt(maxItems.getText()),Integer.parseInt(incrementRecommendation.getText()));
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		getRootPane().setDefaultButton(okButton);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
}
